//
//  UIButton+ACExt.m
//  ACFramework
//
//  Created by caoyq on 2019/1/10.
//

#import "UIButton+ACExt.h"
#import <objc/runtime.h>

@implementation UIButton (ACExt)

@end

@implementation UIButton (ACEnlargeTouchArea)

static char edgeKey;

- (void)ac_setEnlargeEdge: (UIEdgeInsets)edge
{
    objc_setAssociatedObject(self, &edgeKey, [NSValue valueWithUIEdgeInsets:edge], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGRect rect = [self enlargeRect];
    if (CGRectEqualToRect(rect, self.bounds)) {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? self : nil;
}

///扩大范围后的rect
- (CGRect)enlargeRect
{
    NSValue *value = objc_getAssociatedObject(self, &edgeKey);
    if (value) {
        UIEdgeInsets insets = value.UIEdgeInsetsValue;
        return CGRectMake(self.bounds.origin.x - insets.left,
                          self.bounds.origin.y - insets.top,
                          self.bounds.size.width + insets.left + insets.right,
                          self.bounds.size.height + insets.top + insets.bottom);
    }
    return self.bounds;
}

@end
